﻿using Bll;
using DecorationSys.Models;
using Entity;
using Entity.DTO;
using IBll;
using Microsoft.AspNetCore.Mvc;

namespace DecorationSys.Controllers
{
    /// <summary>
    /// 工程分类相关
    /// </summary>
    public class ClassificationController : Controller
    {
        IClassificationBll _classificationBll;

        public ClassificationController(IClassificationBll classificationBll)
        {
            _classificationBll = classificationBll;
        }

        /// <summary>
        /// 工程分类列表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult ClassificationListView()
        {
            return View();
        }

        /// <summary>
        /// 添加工程分类视图
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateClassificationView()
        {
            return View();
        }

        /// <summary>
        /// 修改工程分类视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateClassificationView()
        {
            return View();
        }

        /// <summary>
        /// 工程分类详情视图
        /// </summary>
        /// <returns></returns>
        public IActionResult DetailsView()
        {
            return View();
        }

        /// <summary>
        /// 根据工程分类id获取工程分类信息
        /// </summary>
        /// <param name="classificationId"></param>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetClassificationById(string id)
        {
            var classification = _classificationBll.GetClassificationById(id);

            if (classification == null)
            {
                return MyJsonResult.GetErrorResult("工程分类不存在");
            }
            else
            {
                return MyJsonResult.GetSuccessResult(classification);
            }
        }

        /// <summary>
        /// 更新工程分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult UpdateClassification(string id, string title, string remark)
        {
            //参数验证
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("工程分类id不能为空");
            }
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("工程分类名不能为空");
            }

            string msg;
            //调用业务逻辑层
            bool isSuccess = _classificationBll.UpdateClassification(id, title, remark, out msg);
            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }


        /// <summary>
        /// 获取工程分类列表数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object GetClassificationList(string title, int page, int limit)
        {
            if (page <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "页码有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            if (limit <= 0)
            {
                return new
                {
                    code = 0,
                    msg = "每页条数有误",
                    count = 0,
                    data = new List<object>()
                };
            }

            int count;

            //查工程分类数据
            var list = _classificationBll.GetClassificationList(title, page, limit, out count);

            return new
            {
                code = 0,
                msg = "cg",
                count,
                data = list
            };
        }

        /// <summary>
        /// 创建工程分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult CreateClassification(string title, string remark)
        {
            //参数验证
            if (string.IsNullOrEmpty(title))
            {
                return MyJsonResult.GetErrorResult("请输入工程分类标题");
            }

            string msg;
            bool isSuccess = _classificationBll.CreateClassification(title, remark, out msg);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult(msg);
            }
        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="classificationId"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteClassification(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _classificationBll.DeleteClassification(id);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public MyJsonResult DeleteClassifications(List<string> Ids)
        {
            if (Ids.Count <= 0 || Ids == null)
            {
                return MyJsonResult.GetErrorResult("参数有误");
            }

            bool isSuccess = _classificationBll.DeleteClassifications(Ids);

            if (isSuccess)
            {
                return MyJsonResult.GetSuccessResult();
            }
            else
            {
                return MyJsonResult.GetErrorResult("失败");
            }
        }

        /// <summary>
        /// 获取下拉选备选数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MyJsonResult GetSelectOptionList(string userId)
        {
            //获取下拉选备选数据
            var list = _classificationBll.GetSelectOptionList();
            return MyJsonResult.GetSuccessResult(list);
        }

    }

}
